import { findIndex } from "@/utils/objectUtil"
import { RouteLocationNormalizedLoaded } from "vue-router";

export const useNavTagsView = defineStore("navTagsView", () => {
  const fixationRoute: RouteLocationNormalizedLoaded = {
    fullPath: "/workbench",
    hash: "",
    query: {},
    name: "Workbench",
    path: "/workbench",
    params: {},
    meta: {
      title: "工作台",
      showTitle: "工作台",
      icon: "House",
      fixation: true,
    },
    matched: [],
    redirectedFrom: undefined,
  };

  
  const visitedViews = ref<RouteLocationNormalizedLoaded[]>([]),
    selectedTag = ref<RouteLocationNormalizedLoaded>();
  // 删除tab
  const deleteView = (view: RouteLocationNormalizedLoaded): void => {
    deleteVisitedView(view);
  };
  const deleteVisitedView = (view: RouteLocationNormalizedLoaded): void => {
    for (const [i, v] of visitedViews.value.entries()) {
      if (view.path === v.path) {
        visitedViews.value.splice(i, 1);
        break;
      }
    }
  };
  // 删除左侧
  const deleteLeftViews = (view: RouteLocationNormalizedLoaded) => {
    const index = findIndex<RouteLocationNormalizedLoaded>(
      visitedViews.value,
      (v) => v.path === view.path
    );
    if (index > -1) {
      visitedViews.value = visitedViews.value.filter((v, i) => {
        return v?.meta?.affix || v.path === view.path || i > index || i === 0;
      });
    }
  };
  // 删除右侧
  const deleteRightViews = (view: RouteLocationNormalizedLoaded) => {
    const index = findIndex<RouteLocationNormalizedLoaded>(
      visitedViews.value,
      (v) => v.path === view.path
    );
    if (index > -1) {
      visitedViews.value = visitedViews.value.filter((v, i) => {
        return v?.meta?.affix || v.path === view.path || i < index;
      });
    }
  };
  // 删除其他
  const deleteOthersViews = (view: RouteLocationNormalizedLoaded) => {
    visitedViews.value = visitedViews.value.filter((v, i) => {
      return v?.meta?.affix || v.path === view.path || i === 0;
    });
  };
  // 删除全部
  const deleteAllViews = () => {
    visitedViews.value = visitedViews.value.filter((v) => {
      return v?.meta?.affix;
    });
  };
  // 添加条目
  const addView = (view: RouteLocationNormalizedLoaded): void => {
    addVisitedView(view);
  };
  const addVisitedView = (view: RouteLocationNormalizedLoaded): void => {
    if (visitedViews.value.some((v) => v.path === view.path)) return;
    if (view.path !== "/workbench") {
      if (
        visitedViews.value.length === 0 ||
        (visitedViews.value.length > 0 &&
          visitedViews.value[0].path !== "/workbench")
      ) {
        visitedViews.value.push(fixationRoute);
      }
    }
    let _title = view.meta.title || "no-name";

    view.meta.showTitle = _title;

    if (
      view.path.indexOf("/talentPool/resumeDetail") === 0 ||
      view.path.indexOf("/curriculum/resumeDetail") === 0 ||
      view.path.indexOf("/candidate/resumeDetail") === 0
    ) {
      view.meta.showTitle = view.query.name + "的" + _title;
    }

    visitedViews.value.push(Object.assign({}, view, { title: _title }));
  };

  // 设置当前选中的tag
  const setSelectedTag = (tag: RouteLocationNormalizedLoaded) => {
    selectedTag.value = tag;
  };

  return {
    visitedViews,
    selectedTag,
    addView,
    addVisitedView,
    setSelectedTag,
    deleteView,
    deleteLeftViews,
    deleteRightViews,
    deleteOthersViews,
    deleteAllViews,
  };
});
